package com.yang.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Arg;
import org.apache.ibatis.annotations.Case;
import org.apache.ibatis.annotations.ConstructorArgs;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.mapping.FetchType;

import com.yang.entity.User;
import com.yang.entity.result.Author;
import com.yang.entity.result.Blog;
import com.yang.entity.result.Comment;
import com.yang.sqlBuilder.AnnotationSqlBuilder;

public interface AnnotationMapper {

	@SelectProvider(method = "getUser", type = AnnotationSqlBuilder.class)
	public List<User> getUser(User user);
	@DeleteProvider(method = "deleteUserSql", type = AnnotationSqlBuilder.class)
	public void deleteUserSql(int id);
	@InsertProvider(method = "insertUserSql", type = AnnotationSqlBuilder.class)
	public void insertUserSql(User user);
	@UpdateProvider(method = "updateUserSql", type = AnnotationSqlBuilder.class)
	public void updateUserSql(User user);
	//如果使用fetchtype，需要添加cglib依赖
	//测试中发现构造函数被调用了2次 ??
	@ConstructorArgs({
		@Arg(column="id",javaType=int.class,id=true),
		@Arg(column="title",javaType=String.class)
	})
	@Results(value={
			@Result(property="content",column="content"),
			@Result(property="author",column="author_id",one=@One(select="getAuthor",fetchType=FetchType.LAZY)),
			@Result(property="editor",column="editor_id",one=@One(select="getAuthor",fetchType=FetchType.LAZY)),
			@Result(property="comments",column="id",many=@Many(select="getComments",fetchType=FetchType.LAZY))
	})
	@Select("select * from blog where id = #{id}")
	public Blog getById(int id);
	@Select("select * from author where id=#{id}")
	public Author getAuthor(int id);
	@Select("select * from comment where blog_id=#{id}")
	public List<Comment> getComments(int id);
}
